﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI
{
    public partial class ShiftDetailsForm : Form
    {
        private DateTime m_StartTimeOfShift;
        private DateTime m_EndTimeOfShift;
        private ApplShift m_ShiftRef = null;

        public ShiftDetailsForm(DateTime startTimeOfShift, DateTime endTimeOfShift)
        {
            m_StartTimeOfShift = startTimeOfShift;
            m_EndTimeOfShift = endTimeOfShift;
            InitializeComponent();
            init();
        }

        private void init()
        {
            initShiftFromServer();
            initControlers();
            initDataSources();
            setReadOnlyStatusOfStat();
        }

        private void setReadOnlyStatusOfStat()
        {
            buttonShowStat.Enabled = dataGridViewRegisteredFirst.RowCount > 0 ||
                                     dataGridViewRegisteredSecond.RowCount > 0
                                     || dataGridViewRegisteredThird.RowCount > 0 || dataGridViewManager.RowCount > 0;
        }

        private void initDataSources()
        {
            this.dataGridViewWanted.DataSource =
                WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatAskedForAShiftWithType(m_ShiftRef.Id, eType.WANTED,
                                                                                               ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                                                               ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
            this.dataGridViewForbidden.DataSource =
                 WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatAskedForAShiftWithType(m_ShiftRef.Id, eType.FORBIDDEN,
                                                                                   ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                                                   ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
            this.dataGridViewRegisteredFirst.DataSource =
                WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatGotShiftInStep(m_ShiftRef.Id, eResult.FIRST,
                                                                                   ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                                                   ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
            this.dataGridViewRegisteredSecond.DataSource =
                WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatGotShiftInStep(m_ShiftRef.Id, eResult.SECOND,
                                                                       ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                                       ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
            this.dataGridViewRegisteredThird.DataSource =
                 WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatGotShiftInStep(m_ShiftRef.Id, eResult.THIRD,
                                                                       ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                                       ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
            this.dataGridViewManager.DataSource =
                  WorkerDetailsToShow.GetWorkersDetailsFromApplWorkerList(ScMainUserControlLogicManager.Instance.GetAllWorkersThatGotShiftInStep(m_ShiftRef.Id, eResult.BYMANAGER,
                                                           ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.AddHours(-1),
                                                           ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek));
        }

        private void initControlers()
        {
            this.textBoxCapacity.Text = m_ShiftRef.NumberOfWorkersNeeded.ToString();
            this.textWeekDay.Text = m_StartTimeOfShift.DayOfWeek.ToString();
            this.textBoxName.Text = m_ShiftRef.Name;
            this.textstartDate.Text = m_StartTimeOfShift.ToShortDateString();
            this.textBoxEndDate.Text = m_EndTimeOfShift.ToShortDateString();
            this.textBoxStartTime.Text = m_StartTimeOfShift.ToShortTimeString();
            this.textBoxEndTime.Text = m_EndTimeOfShift.ToShortTimeString();
            if(!(m_StartTimeOfShift > ScMainUserControlLogicManager.Instance.EndTimeOfCurrentWeek))
            {
                buttonUpdateCapacity.Visible = false;
                textBoxCapacity.ReadOnly = true;
            }

        }

        private void initShiftFromServer()
        {
            m_ShiftRef = ScMainUserControlLogicManager.Instance.ShowShiftDetails(m_StartTimeOfShift, m_EndTimeOfShift);
        }

        private void buttonShowStat_Click(object sender, EventArgs e)
        {
            ShiftStatForm ssf = new ShiftStatForm(m_ShiftRef, dataGridViewWanted.RowCount,
                                                  dataGridViewForbidden.RowCount,
                                                  dataGridViewRegisteredFirst.RowCount,
                                                  dataGridViewRegisteredSecond.RowCount,
                                                  dataGridViewRegisteredThird.RowCount, 
                                                  dataGridViewManager.RowCount);
            ssf.ShowDialog();
        }

        private void buttonUpdateCapacity_Click(object sender, EventArgs e)
        {
            m_ShiftRef.NumberOfWorkersNeeded = Convert.ToInt32(textBoxCapacity.Text);
            ScMainUserControlLogicManager.Instance.UpdateShift(m_ShiftRef);
            MessageBox.Show(@"Shift Updated", @"Shift Update Status", MessageBoxButtons.OK, MessageBoxIcon.Information);           
        }
    }

    public class WorkerDetailsToShow
    {
        public string FullName { get; set; }
        public string Id { get; set; }
        public int MaxShiftsNumber { get; set; }

        public static List<WorkerDetailsToShow> GetWorkersDetailsFromApplWorkerList(List<ApplWorker> applWorkers)
        {
            List<WorkerDetailsToShow> workerDetailsToShows = new List<WorkerDetailsToShow>();
            foreach (var applWorker in applWorkers)
            {
                WorkerDetailsToShow workerDetailsToShow = new WorkerDetailsToShow
                                                              {
                                                                  Id = applWorker.PersonalDetails.Id,
                                                                  FullName = applWorker.PersonalDetails.FullName,
                                                                  MaxShiftsNumber = applWorker.MaxShiftsThatCanGive
                                                              };
                workerDetailsToShows.Add(workerDetailsToShow);

            }
            return workerDetailsToShows;
        }
    }
}
